package org.quasar.airNova.presentationLayer;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.quasar.airNova.businessLayer.CalendarDate;
import org.quasar.airNova.businessLayer.CartaoCredito;
import org.quasar.airNova.businessLayer.Cliente;
import javax.swing.JCheckBox;

public class CriarReserva extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField nLugarJTF;
	private Gui gui;
	private JComboBox dataVoo;
	private JComboBox nVoo;
	private JComboBox cartaoCredito;
	private JComboBox nCliente;
	private JCheckBox checkBoxPDF;

	/**
	 * Create the panel.
	 * 
	 * @param gui
	 */
	public CriarReserva(Gui gui) {
		setGui(gui);
		setLayout(new BorderLayout(0, 0));
		this.setSize(500, 350);
		JPanel panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		JLabel lblNewLabel = new JLabel("N\u00FAmero de Cliente");
		lblNewLabel.setBounds(40, 76, 151, 14);
		panel.add(lblNewLabel);

		JLabel lblNewLabel_2 = new JLabel("Data <dd-mm-yy>");
		lblNewLabel_2.setBounds(40, 126, 126, 14);
		panel.add(lblNewLabel_2);

		JLabel lblNewLabel_1 = new JLabel("N\u00FAmero do Voo");
		lblNewLabel_1.setBounds(40, 101, 126, 14);
		panel.add(lblNewLabel_1);

		JLabel lblNmeroCartaoDe = new JLabel("Cartao de Credito");
		lblNmeroCartaoDe.setBounds(40, 151, 126, 14);
		panel.add(lblNmeroCartaoDe);

		JLabel lblLugar = new JLabel("Lugar");
		lblLugar.setBounds(40, 173, 126, 14);
		panel.add(lblLugar);

		nLugarJTF = new JTextField();
		nLugarJTF.setBounds(231, 170, 203, 20);
		panel.add(nLugarJTF);
		nLugarJTF.setColumns(10);

		JButton btnNewButton_2 = new JButton("Apagar Campos");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				nLugarJTF.setText("");
			}
		});
		btnNewButton_2.setBounds(251, 228, 126, 23);
		panel.add(btnNewButton_2);

		JButton btnNewButton = new JButton("Submeter");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (nVoo.getSelectedItem() != null
						&& dataVoo.getSelectedItem() != null
						&& nCliente.getSelectedItem() != null
						&& cartaoCredito.getSelectedItem() != null
						&& nLugarJTF.getText().length() != 0) {
					boolean reserveCreated = AirNova_Database.criarReserva(
							(String) nVoo.getSelectedItem(),
							(String) dataVoo.getSelectedItem(),
							(String) nCliente.getSelectedItem(),
							(String) cartaoCredito.getSelectedItem(),
							nLugarJTF.getText());
					if (reserveCreated && checkBoxPDF.isSelected()) {
						new PDFgenerator((String) nVoo.getSelectedItem(),
								(String) dataVoo.getSelectedItem(),
								(String) nCliente.getSelectedItem(),
								(String) cartaoCredito.getSelectedItem(),
								nLugarJTF.getText()).generate();
					}
				}

			}
		});
		btnNewButton.setBounds(40, 228, 98, 23);
		panel.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("Fechar");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getGui().getTabbedPane().removeTabAt(
						getGui().getTabbedPane().getSelectedIndex());
			}
		});
		btnNewButton_1.setBounds(152, 228, 89, 23);
		panel.add(btnNewButton_1);

		JLabel lblNewLabel_3 = new JLabel("Nova Reserva");
		lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblNewLabel_3.setBounds(30, 24, 112, 32);
		panel.add(lblNewLabel_3);

		dataVoo = new JComboBox();
		dataVoo.setBounds(231, 123, 203, 20);
		panel.add(dataVoo);

		if (Tools.getVoosNumero() != null)
			nVoo = new JComboBox(Tools.getVoosNumero());
		else
			nVoo = new JComboBox();
		nVoo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				generateComboxData((String) nVoo.getSelectedItem());
			}
		});
		nVoo.setBounds(231, 98, 203, 20);
		panel.add(nVoo);

		cartaoCredito = new JComboBox();
		cartaoCredito.setBounds(231, 148, 203, 20);
		panel.add(cartaoCredito);

		if (Tools.getClienteCodigos() != null)
			nCliente = new JComboBox(Tools.getClienteCodigos());
		else
			nCliente = new JComboBox<>();
		nCliente.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				generateComboxCartoes((String) nCliente.getSelectedItem());
			}

		});
		nCliente.setBounds(231, 73, 203, 20);
		panel.add(nCliente);

		checkBoxPDF = new JCheckBox("");
		checkBoxPDF.setBounds(230, 198, 21, 23);
		panel.add(checkBoxPDF);

		JLabel lblImprimirPdf = new JLabel("Gerar PDF");
		lblImprimirPdf.setBounds(40, 203, 75, 14);
		panel.add(lblImprimirPdf);

	}

	public Gui getGui() {
		return gui;
	}

	private void generateComboxData(String nVoo) {
		CalendarDate[] cds = Tools.getVoosData(nVoo);
		addDatasComboBox(cds);
	}

	private void addDatasComboBox(CalendarDate[] cds) {
		dataVoo.removeAllItems();
		for (int i = 0; i < cds.length; i++) {
			dataVoo.addItem("" + cds[i].day() + "-" + cds[i].month() + "-"
					+ cds[i].year());
		}
	}

	private void generateComboxCartoes(String clientNumber) {
		Cliente c = Tools.getCliente(clientNumber);
		Set<CartaoCredito> cartoes = Tools.getCartoesDoCliente(c);
		addCartoesComboBox(cartoes);
	}

	private void addCartoesComboBox(Set<CartaoCredito> cartoes) {
		cartaoCredito.removeAllItems();
		for (CartaoCredito each : cartoes) {
			cartaoCredito.addItem("" + each.numero());
		}
	}

	public void setGui(Gui gui) {
		this.gui = gui;
	}
}
